//
//  HomeViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()
/** 背景图 **/
@property(nonatomic,strong) UIImageView *backbroundView;
@property(nonatomic,strong) UILabel *tipsLabel;


/** 配对按钮 **/
@property(nonatomic,strong) UIButton *matchButton;

/** 前往商城按钮 **/
@property(nonatomic,strong) UIButton *goToShopButton;



@property (strong, nonatomic) UIButton *lostButton;
@property (strong, nonatomic) UIButton *seekButton;
@property (strong, nonatomic) UIButton *notDisturbButton;
/** 设备视图 **/
@property(nonatomic,strong) MachineView *machineView;

@property(nonatomic,strong) UIView *isMachineView;
@property(nonatomic,strong) UIView *noMachineView;

@property(nonatomic, strong) NSMutableArray *deviceList ;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
}

- (void)refreshBaseControlValue {
    [self setNavigationTitle:@"我的水杯" titleColor:[UIColor whiteColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F9"];
    [self layoutSubviews];
    

}

- (void)viewWillAppear:(BOOL)animated {

    if ([DTCEXRACT_OBJECT(MATCH) isEqualToString:@"YES"]) {
#pragma mark -- 有设备
        [self.deviceList removeAllObjects];
        [_deviceList addObjectsFromArray:[[DTCBluetoothDataBaseManager sharedInstance]getAllDevice]];
//        NSLog(@"device=%@",_deviceList);
        if (_deviceList.count > 0) {
            self.machineView.device = _deviceList[0];
        }
        [self.view bringSubviewToFront:self.isMachineView];
    } else {
#pragma mark -- 没设备
        [self.view bringSubviewToFront:self.noMachineView];
    }


}

- (void)layoutSubviews {
    __weak UIView *superView = self.view;
    
    [self.isMachineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.noMachineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.machineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.width.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(740));
    }];

    CGFloat padding = px1080Width(48);
    NSMutableArray *views = [NSMutableArray array];
    [views addObjectsFromArray:@[self.lostButton,self.seekButton,self.notDisturbButton]];
    [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:px1080Width(280) leadSpacing:padding tailSpacing:padding];
    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.machineView.mas_bottom).with.offset(px1920Height(40));
        UIView *blueView = (UIView *)views[0];
        make.height.mas_equalTo(blueView.mas_width);
    }];


    [self.backbroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(272));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(456));
        make.height.mas_equalTo(px1920Height(456));
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backbroundView.mas_bottom).with.offset(px1920Height(40));
        make.width.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(73));
    }];
    
    [self.matchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tipsLabel.mas_bottom).with.offset(px1920Height(139));
        make.left.mas_equalTo(superView).with.offset(px1080Width(45));
        make.right.mas_equalTo(superView).with.offset(-px1080Width(45));
        make.height.mas_equalTo(px1920Height(148));
    }];
    
    
    [self.goToShopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.matchButton.mas_bottom).with.offset(px1920Height(40));
        make.left.mas_equalTo(superView).with.offset(px1080Width(45));
        make.right.mas_equalTo(superView).with.offset(-px1080Width(45));
        make.height.mas_equalTo(px1920Height(148));
    }];


    


}

#pragma mark -- 事件
- (void)didClickLostButton {
    self.hidesBottomBarWhenPushed = YES;
    LostViewController *vc = [[LostViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didClickSeekButton {
    self.hidesBottomBarWhenPushed = YES;
    SeekViewController *vc = [[SeekViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didClickNotDisturbButton {

    
    self.hidesBottomBarWhenPushed = YES;
    NotDisturbViewController *vc = [[NotDisturbViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didClickMatchButton {
    self.hidesBottomBarWhenPushed = YES;
    SeekMachineViewController *vc = [[SeekMachineViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}

- (void)didClickGoToShopButton {
    NSString *shopLink = @"http://m.tb.cn/ew2wB";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:shopLink]];
}

#pragma mark -- 懒加载

- (UIButton *)lostButton
{
    if(!_lostButton)
    {
        UIView *superView = self.isMachineView;
        _lostButton = [[UIButton alloc]init];
        [superView addSubview:_lostButton];
        [_lostButton setImage:[UIImage imageNamed:@"button_lost_nor"] forState:UIControlStateNormal];
        [_lostButton setImage:[UIImage imageNamed:@"button_lost_sel"] forState:UIControlStateHighlighted];
        [_lostButton addTarget:self action:@selector(didClickLostButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lostButton;
}
- (UIButton *)seekButton
{
    if(!_seekButton)
    {
        UIView *superView = self.isMachineView;
        _seekButton = [[UIButton alloc]init];
        [superView addSubview:_seekButton];
        [_seekButton setImage:[UIImage imageNamed:@"button_seek_nor"] forState:UIControlStateNormal];
        [_seekButton setImage:[UIImage imageNamed:@"button_seek_sel"] forState:UIControlStateHighlighted];
        [_seekButton addTarget:self action:@selector(didClickSeekButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _seekButton;
}
- (UIButton *)notDisturbButton
{
    if(!_notDisturbButton)
    {
        UIView *superView = self.isMachineView;
        _notDisturbButton = [[UIButton alloc]init];
        [superView addSubview:_notDisturbButton];
        [_notDisturbButton setImage:[UIImage imageNamed:@"button_notdisturb_nor"] forState:UIControlStateNormal];
        [_notDisturbButton setImage:[UIImage imageNamed:@"button_notdisturb_sel"] forState:UIControlStateHighlighted];
        [_notDisturbButton addTarget:self action:@selector(didClickNotDisturbButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _notDisturbButton;
}

- (MachineView *)machineView
{
    if(!_machineView)
    {
        UIView *superView = self.isMachineView;
        _machineView = [[MachineView alloc]init];
        [superView addSubview:_machineView];
    }
    return _machineView;
}

- (UIButton *)matchButton
{
    if(!_matchButton)
    {
        UIView *superView = self.noMachineView;
        _matchButton = [[UIButton alloc]init];
        [superView addSubview:_matchButton];
        [_matchButton setTitle:@"已有智能设备，现在配对" forState:UIControlStateNormal];
        [_matchButton setBackgroundImage:[UIImage imageNamed:@"button_login_nor"] forState:UIControlStateNormal];
        [_matchButton setBackgroundImage:[UIImage imageNamed:@"button_login_sel"] forState:UIControlStateSelected];
        [_matchButton addTarget:self action:@selector(didClickMatchButton) forControlEvents:UIControlEventTouchUpInside];
        [_matchButton.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(52)]];
    }
    return _matchButton;
}
- (UIButton *)goToShopButton
{
    if(!_goToShopButton)
    {
        UIView *superView = self.noMachineView;
        _goToShopButton = [[UIButton alloc]init];
        [superView addSubview:_goToShopButton];
        [_goToShopButton setTitle:@"去商城看看？" forState:UIControlStateNormal];
        [_goToShopButton setTitleColor:[UIColor colorWithHexString:@"#3e76F6"] forState:UIControlStateNormal];
        [_goToShopButton.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(52)]];
        [_goToShopButton addTarget:self action:@selector(didClickGoToShopButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goToShopButton;
}

- (UILabel *)tipsLabel
{
    if(!_tipsLabel)
    {
        UIView *superView = self.noMachineView;
        _tipsLabel = [[UILabel alloc]init];
        [superView addSubview:_tipsLabel];
        [_tipsLabel setFont:[UIFont systemFontOfSize:px1080Width(52)]];
        [_tipsLabel setTextColor:[UIColor colorWithHexString:@"#585D6D"]];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.text = @"您好像还没设备哟";
    }
    return _tipsLabel;
}

- (UIImageView *)backbroundView
{
    if(!_backbroundView)
    {
        UIView *superView = self.noMachineView;
        _backbroundView = [[UIImageView alloc]init];
        [superView addSubview:_backbroundView];
        _backbroundView.image = [UIImage imageNamed:@"bg_equipment"];
    }
    return _backbroundView;
}

- (UIView *)isMachineView
{
    if(!_isMachineView)
    {
        UIView *superView = self.view;
        _isMachineView = [[UIView alloc]init];
        _isMachineView.backgroundColor = [UIColor whiteColor];
        [superView addSubview:_isMachineView];
    }
    return _isMachineView;
}

- (UIView *)noMachineView
{
    if(!_noMachineView)
    {
        UIView *superView = self.view;
        _noMachineView = [[UIView alloc]init];
        _noMachineView.backgroundColor = [UIColor whiteColor];
        [superView addSubview:_noMachineView];
    }
    return _noMachineView;
}

- (NSMutableArray *)deviceList
{
    if(!_deviceList)
    {
        _deviceList = [[NSMutableArray alloc]init];
    }
    return _deviceList;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
